package com.qianguo.buyer;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.eva.androidlibrary.image.ImageLoader;
import com.eva.androidlibrary.utils.SharedPreferencesHelper;
import com.eva.androidlibrary.widget.pageindicator.CirclePageIndicator;
import com.qianguo.buyer.base.BaseNoTitleActivity;
import com.qianguo.buyer.constant.SpKeys;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 买家引导页
 *
 * @author GUXIA318
 * @date 2015-09-17 15:46
 */
@Fullscreen
@EActivity(R.layout.activity_buyer_guide)
public class BuyerGuideActivity extends BaseNoTitleActivity {

    private static final int[] GUIDE_IMAGE_RES_IDS = new int[] {R.mipmap.welcome1,
            R.mipmap.welcome2, R.mipmap.welcome3, R.mipmap.welcome4};

    @ViewById(R.id.welcome_viewpager)
    ViewPager mViewPager;

    @ViewById(R.id.welcome_indicator)
    CirclePageIndicator mIndicator;

    public static void jumpToGuide(Context context) {
        context.startActivity(new Intent(context, BuyerGuideActivity_.class));
    }

    private void initIndicator() {
        mIndicator.setFillColor(getResources().getColor(R.color.primary_color));
        mIndicator.setPageColor(getResources().getColor(R.color.light_primary_color));
        mIndicator.setStrokeWidth(0);
    }

    @Override
    public void finish() {
        // 记录已启动过App
        SharedPreferencesHelper.getInstance(this).putBoolean(SpKeys.IS_FIRST_LAUNCH, false);
        super.finish();
    }

    @Override
    protected void initializeUI() {
        List<View> lists = new ArrayList<>();
        for (int i = 0; i < GUIDE_IMAGE_RES_IDS.length; i++) {
            View view = getLayoutInflater().inflate(R.layout.layout_guide, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            if (i == GUIDE_IMAGE_RES_IDS.length - 1) {
                TextView enterIn = (TextView) view.findViewById(R.id.enter_app);
                enterIn.setVisibility(View.VISIBLE);
                enterIn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BuyerMainActivity.jumpToMain(BuyerGuideActivity.this);
                        finish();
                    }
                });
            }
            ImageLoader.getInstance(this).loadLargeResImage(this, imageView, GUIDE_IMAGE_RES_IDS[i]);
            lists.add(view);
        }

        initIndicator();
        mViewPager.setAdapter(new GuideAdapter(lists));
        mIndicator.setViewPager(mViewPager);
    }

    public class GuideAdapter extends PagerAdapter {

        private List<View> mViews;

        public GuideAdapter(List<View> views) {
            this.mViews = views;
        }

        @Override
        public int getCount() {
            return mViews == null ? 0 : mViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViews.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViews.get(position));
            return  mViews.get(position);
        }
    }
}
